<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require_once(dirname(__FILE__).'/smarty/libs/Smarty.class.php');

class Tpl extends Smarty{
	function tpl(){
		 $smarty = new Smarty;
		 $this->template_dir = realpath(dirname(__FILE__).'/../../application/views/');
		 $this->cache_dir = realpath(dirname(__FILE__).'/../../application/cache/');
		 $this->compile_dir = realpath(dirname(__FILE__).'/../../application/templates_c/');
		 $this->left_delimiter = "{{"; 
		 $this->right_delimiter = "}}";
		 $this->compile_check = true;
		 $this->debugging = false;
		 $this->caching = false;	//是否使用缓存，项目调试期间，不建议启用
	
	}
	
	function display($resource_name,$cache_id = null, $compile_id = null,$swich =null){
		$logTimer=date("Y-m-d H:i:s");
		$this -> assign("loginTimer",$logTimer);
		$this -> assign("userid",$_COOKIE["userid"]);
		$this -> assign("username",$_COOKIE["username"]);
		$this -> assign("userclassid",$_COOKIE["classid"]);
		$this -> assign("userdepartment",$_COOKIE["department"]);
		$this->CI =& get_instance();
		$this->CI->load->database();
		//echo $this->CI->config->item('overseadetail');
		//$this -> assign("user_login1",$_COOKIE["user_login"]);
		
		if($_SERVER["REQUEST_URI"] == "/login"  || $swich == 1)
        {  
		}else{
			//开始判断访问权限
			$ex = explode("?",$_SERVER["REQUEST_URI"]);
			$ex= $ex[0];
			$rsdb = $this->CI->db->get_one("SELECT * FROM d_menu WHERE value_fied='".$ex."'");	//这里可以判断是否有权限访问
			require(dirname(__FILE__)."/../config/"."menu.php");	//加载栏目文件,
			//判断显示权限
			if($_COOKIE["power"]=='all')	//主管级不设访问权限
			{
				 $this -> assign("authList",'all');		//权限
				 $this -> assign("isshowauth",'all');//有权查看到的1级目录
				 $this -> assign("twoisshowauth",'all');//有权查看到的2级目录
				 $this -> assign("threehowauth",'all');//有权查看到的3级目录
			}else{
				
				if(!$_COOKIE["power"])
				{
					die("请先联系管理员设置权限！<a href='/'>点击重新登录</a>");
				}
				$isshowauth=array();
				$twoisshowauth=array();
				$threehowauth=array();
				$query = $this->CI->db->query("SELECT * FROM d_menu WHERE id in($_COOKIE[power])");
				$rules = $query->result_array();
				foreach ($rules as $key=> $rule) {
					//只要存在就记录
					if($rule['rank']==1)	
					{
						$isshowauth[] = $rule['value_txt'];
					}
					if($rule['rank']==2)	
					{
						$twoisshowauth[] = $rule['value_txt'];
					}
					if($rule['rank']==3)	
					{
						$threehowauth[] = $rule['value_txt'];
					}
				 }
				

				 $this -> assign("isshowauth",$isshowauth);//有权查看到的1级目录
				 $this -> assign("twoisshowauth",$twoisshowauth);//有权查看到的2级目录
				 $this -> assign("threehowauth",$threehowauth);//有权查看到的3级目录
			}
			$this -> assign("menudb",$menudb);
			$this -> assign("currentfile",$ex);
			$this -> assign("base_menuName",$base_menuName);
			
			//当前需要上架的商品信息
			
			    $userid = $_COOKIE["userid"];
				$sql = "SELECT name,sku,A.id,linkUrl ,B.goods_id,C.thumb_url FROM  db_product_develop  A LEFT JOIN db_goods B on B.goods_sku = A.sku LEFT JOIN db_goods_images C on B.goods_id = C.goods_id WHERE 1  AND  ( return_receivers not like '%{$userid}%' or return_reason IS NULL) AND receivers like '%,".$userid.",%' GROUP BY sku  order by A.id DESC";
				$query = $this->CI->db->query($sql);
				$newproductlist = $query->result_array();
				$this -> assign("newproductlist",$newproductlist);
		
			//当前需要上架的商品信息结束
			/*******************提示信息设置******************************/
			if($_COOKIE["power"]){
				$showpower = explode(",",$_COOKIE["power"]);
				$this -> assign("showpower",$showpower);
				if($_COOKIE["power"]=='all')
				$this -> assign("managers",1);	//管理者
			}
			$this -> assign("isclosetips",$this->CI->config->item('isclosetips'));	//自动设置关闭
			$message = $this->CI->message->getMessageslist($_COOKIE["userid"]);
			if(!$message["messagetotal"]) $message["messagetotal"]=0;
			$this -> assign("messagelist",$message["messageslist"]);
			$this -> assign("messageNum",$message["messagetotal"]);
			$this->display("public/header.tpl", $cache_id, $compile_id, true);
		}

		parent::display($resource_name, $cache_id = null, $compile_id = null);
	}
	function getfirstcate($id)
	{
		$this->CI =& get_instance();
		$this->CI->load->database();
		$rsdb = $this->CI->db->get_one("SELECT * FROM d_menu WHERE id='".$id."'");
		if($rsdb['sort_id'] != 0) return $this->getfirstcate($rsdb['sort_id']);
		return $rsdb['power'];
	}
}